﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.WebSite.DelegateAndArges;

namespace LEARNREN.COM.WebSite.UserDefinedControls
{
    public partial class CatalogueTree : System.Web.UI.UserControl
    {
        /// <summary>
        /// 可以点击的节点类型
        /// </summary>
        public CatalogueTypeEnum CanClickNodeType { get; set; }

        /// <summary>
        /// 处理逻辑用的对象
        /// </summary>
        public CatalogueLogic CatalogueLogicProvider { get; set; }

        /// <summary>
        /// 是否可以点击根节点
        /// </summary>
        public bool CanClickRootNode { private get; set; }

        /// <summary>
        /// 是否显示栏目的Id
        /// </summary>
        public bool IsShowId { get; set; }

        /// <summary>
        /// 选中节点改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public event CatalogueTreeSelectedNodeChangedHandle SelectedNodeChanged;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitTree();
            }
        }

        /// <summary>
        /// 加载树
        /// </summary>
        private void InitTree()
        {
            TreeViewCatalogue.Nodes.Clear();
            List<Catalogue> catalogueList = CatalogueLogicProvider.GetList();
            TreeNode rootNode = new TreeNode("根节点", "0");
            
            AppendIdText(rootNode);

            if (CanClickRootNode == false)
            {
                rootNode.SelectAction = TreeNodeSelectAction.None;
            }
            Catalogue catalogue = new Catalogue { Title = "根节点", Id = 0 };
            InitNode(rootNode, catalogue, catalogueList);
            TreeViewCatalogue.Nodes.Add(rootNode);
            TreeViewCatalogue.ExpandAll();
        }

        /// <summary>
        /// 根据是否需要显示Id信息向节点添加Id值
        /// </summary>
        /// <param name="rootNode"></param>
        private void AppendIdText(TreeNode rootNode)
        {
            if (IsShowId)
            {
                rootNode.Text += string.Format("(Id:{0})", rootNode.Value);
            }
        }

        /// <summary>
        /// 选择项改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TreeViewCatalogue_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (this.SelectedNodeChanged != null)
            {
                CatalogueTreeSelectedNodeChangedEventArgs arg = new CatalogueTreeSelectedNodeChangedEventArgs();
                arg.SelectedCatalogueId = long.Parse(TreeViewCatalogue.SelectedValue);

                SelectedNodeChanged(sender, arg);
            }
        }

        /// <summary>
        /// 加载节点
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="catalogue"></param>
        /// <param name="catalogueList"></param>
        private void InitNode(TreeNode parent, Catalogue catalogue, List<Catalogue> catalogueList)
        {
            if (catalogue == null)
            {
                return;
            }

            long id = catalogue.Id;
            var children = from d in catalogueList where d.ParentId == id orderby d.Order select d;
            foreach (var childCatalogue in children)
            {
                TreeNode node = new TreeNode(childCatalogue.Title, childCatalogue.Id.ToString());

                AppendIdText(node);

                if (this.CanClickNodeType != CatalogueTypeEnum.All)
                {
                    if (childCatalogue.CatalogueType == this.CanClickNodeType)
                    {
                        node.SelectAction = TreeNodeSelectAction.Select;
                    }
                    else
                    {
                        node.SelectAction = TreeNodeSelectAction.None;
                    }
                }

                parent.ChildNodes.Add(node);
                InitNode(node, childCatalogue, catalogueList);
            }
        }

        /// <summary>
        /// 重新加载树
        /// </summary>
        public void Reload()
        {
            InitTree();
        }
    }
}